﻿using FadeJSON;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;

namespace Work.Weixin
{
    public class wxOA
    {
        private static readonly HttpClient httpClient = new HttpClient();
        private wxCorporation corporation;
        /// <summary>
        /// 企业微信内置的审批app
        /// </summary>
        private string oaSecret;
        private IAccessTokenCache oaTokenCache;

        public wxOA(wxCorporation corp, string oaSecret, IAccessTokenCache oaTokenCache)
        {
            if (corp == null
                || string.IsNullOrWhiteSpace(oaSecret)
                || oaTokenCache == null)
            { throw new ArgumentNullException(); }

            this.corporation = corp;
            this.oaSecret = oaSecret;
            this.oaTokenCache = oaTokenCache;
        }

        public string GetAccessToken()
        {
            string access_token = this.corporation.GetAccessToken(oaTokenCache, oaSecret);
            return access_token;
        }

        /// <summary>
        /// 获取审批模板详情
        /// </summary>
        /// <param name="template_id"></param>
        /// <returns></returns>
        public string GetTemplateDetail(string template_id)
        {
            string access_token = this.corporation.GetAccessToken(oaTokenCache, oaSecret);
            string uri = string.Format(
           @"https://qyapi.weixin.qq.com/cgi-bin/oa/gettemplatedetail?access_token={0}",
                               access_token);
            StringContent content;
            {
                var jsonObj = new JsonObject();
                jsonObj.Add("template_id", template_id);
                var json = jsonObj.ToString();
                content = new StringContent(json);
            }
            string strJson;
            {
                var response = httpClient.PostAsync(uri, content).Result;
                var responseString = response.Content.ReadAsStringAsync().Result;
                JsonBaseType jsonObj = Json.FromString(responseString);
                strJson = jsonObj.ToString();
            }
            return strJson;
        }

        /// <summary>
        /// 批量获取审批单号
        /// </summary>
        /// <param name="startTime"></param>
        /// <param name="endTime"></param>
        /// <param name="cursor"></param>
        /// <returns></returns>
        public string GetApprovalInfo(DateTime startTime, DateTime endTime, int cursor = 0)
        {
            string access_token = this.corporation.GetAccessToken(oaTokenCache, oaSecret);
            string uri = string.Format(
           @"https://qyapi.weixin.qq.com/cgi-bin/oa/getapprovalinfo?access_token={0}",
                               access_token);
            StringContent content;
            {
                var jsonObj = new JsonObject();
                jsonObj.Add("starttime", startTime.ToUnixTimestamp().ToString());
                jsonObj.Add("endtime", endTime.ToUnixTimestamp().ToString());
                jsonObj.Add("cursor", cursor);
                var json = jsonObj.ToString();
                content = new StringContent(json);
            }
            string strJson;
            {
                var response = httpClient.PostAsync(uri, content).Result;
                var responseString = response.Content.ReadAsStringAsync().Result;
                JsonBaseType jsonObj = Json.FromString(responseString);
                strJson = jsonObj.ToString();
            }
            return strJson;
        }

        /// <summary>
        /// 获取审批申请详情
        /// </summary>
        /// <param name="sp_no">201909270001</param>
        /// <returns></returns>
        public string GetApprovalDetail(string sp_no)
        {
            string access_token = this.corporation.GetAccessToken(oaTokenCache, oaSecret);
            string uri = string.Format(
           @"https://qyapi.weixin.qq.com/cgi-bin/oa/getapprovaldetail?access_token={0}",
                               access_token);
            StringContent content;
            {
                var jsonObj = new JsonObject();
                jsonObj.Add("sp_no", sp_no);
                var json = jsonObj.ToString();
                content = new StringContent(json);
            }
            string strJson;
            {
                var response = httpClient.PostAsync(uri, content).Result;
                var responseString = response.Content.ReadAsStringAsync().Result;
                JsonBaseType jsonObj = Json.FromString(responseString);
                strJson = jsonObj.ToString();
            }
            return strJson;
        }

        /// <summary>
        /// 提交审批申请
        /// </summary>
        /// <param name="json"></param>
        public string ApplyEvent(string json)
        {
            string access_token = this.corporation.GetAccessToken(oaTokenCache, oaSecret);
            string uri = string.Format(
           @"https://qyapi.weixin.qq.com/cgi-bin/oa/applyevent?access_token={0}",
                               access_token);
            StringContent content;
            {
                content = new StringContent(json);
            }
            string strJson;
            {
                var response = httpClient.PostAsync(uri, content).Result;
                var responseString = response.Content.ReadAsStringAsync().Result;
                JsonBaseType jsonObj = Json.FromString(responseString);
                strJson = jsonObj.ToString();
            }

            return strJson;
        }


    }
}
